<?php
$admin->checkadminpriv('allowadminlead');
!$opration && $opration = 'list';
cpheader();
if ($opration=='save'){
	$_POST['leadid'] = isset($_POST['leadid']) ? intval($_POST['leadid']) : 0;
	$_POST['name'] = isset($_POST['name']) ? trim($_POST['name']) : 0;
	$_POST['title'] = isset($_POST['title']) ? trim($_POST['title']) : '';
	$_POST['avatar'] = isset($_POST['avatar']) ? trim($_POST['avatar']) : '';
	$_POST['resp'] = isset($_POST['resp']) ? trim($_POST['resp']) : '';
	$_POST['body'] = isset($_POST['body']) ? trim($_POST['body']) : '';
	if ($_POST['leadid']>0){
		$db->query("UPDATE sdw_leads SET name='$_POST[name]',title='$_POST[title]',avatar='$_POST[avatar]',resp='$_POST[resp]',body='$_POST[body]',author='$_SESSION[admin]',authorid='$_SESSION[adminid]',dateline='$timestamp' WHERE leadid=".$_POST['leadid']);
		$links[0] = array('text'=>$LANG['reedit'],'href'=>'index.php?action=lead&edit&leadid='.$_POST['leadid']);
		$links[1] = array('text'=>$LANG['back_list'],'href'=>'index.php?action=lead');
		showmsg('edit_success',0,$links);
	}else {
		$db->query("INSERT INTO sdw_leads(name,title,avatar,resp,body,author,authorid,dateline)VALUES
		('$_POST[name]','$_POST[title]','$_POST[avatar]','$_POST[resp]','$_POST[body]','$_SESSION[admin]','$_SESSION[adminid]','$timestamp')");
		$links[0] = array('text'=>$LANG['continue_add'],'href'=>'index.php?action=lead&opration=addnew');
		$links[1] = array('text'=>$LANG['back_list'],'href'=>'index.php?action=lead');
		showmsg('save_success',0,$links);
	}
}

if ($opration=='drop'){
	$leadid = isset($_GET['id']) ? trim($_GET['id']) : 0;
	$db->query("DELETE FROM sdw_lead_about WHERE leadid IN ($leadid)");
	//$db->query("DELETE FROM sdw_leadmails WHERE leadid IN ($leadid)");
	$db->query("DELETE FROM sdw_leads WHERE leadid IN ($leadid)");
}

if ($opration=='addnew'){
	$smarty->assign('editor',get_editor('body'));
}

if ($opration=='edit'){
	$leadid = isset($_GET['leadid']) ? intval($_GET['leadid']) : 0;
	$lead = $db->get_one("SELECT * FROM sdw_leads WHERE leadid=$leadid");
	$smarty->assign('lead',$lead);
	$smarty->assign('editor',get_editor('body',$lead['body']));
}

if ($opration=='list' || $inajax){
	$leads = array();
	$query = $db->query("SELECT leadid,name,title,resp FROM sdw_leads ORDER BY leadid ASC");
	while ($result = $db->fetch_array($query)){
		$result['resp'] = cutstr($result['resp'],60,'...');
		$leads[] = $result;
	}
	$smarty->assign('leads',$leads);
}
$smarty->assign('opration',$opration);
$smarty->display('admin_lead.htm');
cpfooter();
?>